<template>
  <div>
    <el-button @click="showDrawer" type="primary" style="margin-left: 16px;">
      点我打开
    </el-button>
    
    <el-drawer title="我是标题" :visible.sync="drawer">

      <div style="margin: 30px">
        <el-input v-model="title" placeholder="输入提交内容" />
      </div>

      <div>
        <el-date-picker
          v-model="time"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </div>

      <div style="margin: 30px;">
        <el-button type="primary" style="margin-left: 16px;" @click="subForm">提交</el-button>
        <el-button type="primary" style="margin-left: 16px;" @click="subEditForm" v-show="this.chapterId">保存</el-button>
      </div>

    </el-drawer>
  
  
    <el-table :data="List" style="width: 100%">
      <el-table-column label="日期" width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" width="380">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>标题: {{ scope.row.chapterTitle }}</p>
            <p>日期: {{ scope.row.time }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.chapterTitle }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  
</template>
  
<script>
  export default {
    name:'ListView',
    data() {
      return {
        List:[],
        time: "",
        drawer: false,
        title: ""
      }
    },
    methods:{
      handleEdit(row) {
        this.drawer = true;
        this.chapterId = row.chapterId;
        this.chapterTitle = row.chapterTitle;
        this.timer = row.time;
      },
      async handleDelete(index, row) {
        let Id = row.chapterId;
        let result = await this.$axios.post("/delete", { chapterId: Id })

        if (result.code === 200) {
            this.List.splice(index, 1);
        }

        history.go(0)
      },
      showDrawer() {
            this.drawer = true;
            this.chapterId = "";
            this.chapterTitle = "";
            this.timer = "";
        },
      async subEditForm() {
        let result = await this.$axios.post("/subEditForm", { chapterId: this.chapterId, chapterTitle: this.chapterTitle, time: this.time })

        console.log(result)
      },
      async getList() {
        let result = await this.$axios.get("/getList")

        this.List = result.data
      },
      async subForm() {
          let result = await this.$axios.post("/subForm", { chapterTitle: this.title, time: this.time })

          if (result.data.code === 200) {
                  this.drawer = false;
                  this.getList();
              }
      }
    },
    mounted() {
      this.getList();
    }
  }
</script>